//the butterfly flapped its weak wings, sending the unimportant message from the ignored corner with hope and determination...
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define double long double
#define endl '\n'
int cas,n,a[200009],dp[200009],lst[1000009],pre[200009];
signed main(){
    freopen("color.in","r",stdin);
    freopen("color.out","w",stdout);
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    cin>>cas;
    while(cas--){
        cin>>n;int ans=0;
        for(int i=1;i<=n;i++)cin>>a[i],dp[i]=0;
        for(int i=1;i<=1000000;i++)lst[i]=0;
        for(int i=1;i<=n;i++)pre[i]=pre[i-1]+(a[i]==a[i-1])*a[i];
        for(int i=1;i<=n;i++){
            if(lst[a[i]]){
                if(a[i]==a[i-1])dp[i]=dp[i-1]+a[i];
                else dp[i]=max(dp[i-1],dp[lst[a[i]]+1]+a[i]+pre[i-1]-pre[lst[a[i]]+1]);
            }else dp[i]=dp[i-1];
            ans=max(ans,dp[i]);
            lst[a[i]]=i;
        }
        cout<<ans<<endl;
    }
    return 0;
}